package cn.restcloud.example.controller.redis;

import java.util.List;

import org.bson.Document;

import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.util.PrintUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbUtil;
import cn.restcloud.framework.core.util.db.redis.JRedisUtil;
import cn.restcloud.framework.core.util.db.redis.RedisUtil;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

/**
 * RedisUtil工具类使用示例
 * RedisUtil工具类是支持Redis集群模式的,JRedisUtil工具类是只支持单机模式的
 * @author restcloud
 *
 */
@BeanConfig(appId = "example",beanId = "ShardedRedisUtilDemoRest",beanName = "Redis数据库读写示例",beanType = BeanType.CONTROLLER,autowired = true,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/redis")
public class RedisUtilDemoRest {
	
	@RestConfig(configName = "保存数据到Redis中",method = MethodType.POST,url = "/string/save")
	@Params({
		@Param(id = "key", name = "关键字",required=true),
		@Param(id = "value", name = "要保存的值",required=true),
	})
	public DocAndView saveString(String key,String value) throws Exception{
		String r=RedisUtil.set(key, value);
		return ViewUtil.getSuccessView(r);
	}
	
	@RestConfig(configName = "读取Redis数据",method = MethodType.GET,url = "/string/get")
	@Params({
		@Param(id = "key", name = "关键字",required=true),
	})
	public DocAndView getString(String key) throws Exception{
		String r=JRedisUtil.get(key);
		return ViewUtil.getSuccessView(r);
	}
	
	@RestConfig(configName = "保存Document数据到Redis中",method = MethodType.POST,url = "/document/save")
	@Params({
		@Param(id = "key", name = "关键字",required=true),
		@Param(id = "value", name = "要保存的值",required=true),
	})
	public DocAndView saveDocument(String key,String value) throws Exception{
		Document doc=new Document();
		doc.put("fieldId", value);
		String r=RedisUtil.set(key, doc);
		return ViewUtil.getSuccessView(r);
	}
	
	@RestConfig(configName = "读取Redis中的Document对像",method = MethodType.GET,url = "/document/get")
	@Params({
		@Param(id = "key", name = "关键字",required=true),
	})
	public DocAndView getDocument(String key) throws Exception{
		Document doc=RedisUtil.getDocument(key);
		return ViewUtil.getJsonView(doc);
	}
	
	@RestConfig(configName = "保存List<Document>数据到Redis中",method = MethodType.POST,url = "/documents/save")
	public DocAndView saveDocuments() throws Exception{
		List<Document> docs=RdbUtil.listDocs("select top 10 * from bpm_maindata");//从sql中读取数据存入到 redis中
		RedisUtil.setList("TestDocuments", docs);
		return ViewUtil.getSuccessView("设置成功");
	}
	
	@RestConfig(configName = "读取Redis中的List<Document>对像",method = MethodType.GET,url = "/documents/get")
	public DocAndView getDocuments() throws Exception{
		List<Document> docs=RedisUtil.getList("TestDocuments");
		return ViewUtil.getJsonView(docs);
	}
	
	@RestConfig(configName = "获取Keys", method = MethodType.GET, url = "/scan")
	public DocAndView RedisTest() throws Exception {
		PrintUtil.o("开始修改Redis");
		Jedis jredis = JRedisUtil.getJedis("redis");
		
		String cursor = ScanParams.SCAN_POINTER_START;
	    String key = "*";
	    ScanParams scanParams = new ScanParams();
	    scanParams.match(key);// 匹配以* 为前缀的 key
	    scanParams.count(3);
	    
	    //while (true){
	        //使用scan命令获取500条数据，使用cursor游标记录位置，下次循环使用
	        ScanResult<String> scanResult = jredis.scan(cursor, scanParams);
	        cursor = scanResult.getCursor();// 返回0 说明遍历完成
	        List<String> list = scanResult.getResult();
	        long t1 = System.currentTimeMillis();
	        for(String keyId:list) {
	        	PrintUtil.o("key=="+keyId);
	        }
	        long t2 = System.currentTimeMillis();
	        System.out.println("耗时: " + (t2-t1) + "毫秒,cursor:" + cursor);
	        if ("0".equals(cursor)){
	           // break;
	        }
	    //}
	    
        
		return ViewUtil.getSuccessView("ok");
	}
	
	
}
